package kr.hkit.powerjava.chapter22.work2.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.hkit.powerjava.chapter22.work2.vo.Student;

public class StudentManagerImpl implements ItemDaoInterface<Student> {

	private ArrayList<Student> arList;
	
	public StudentManagerImpl(ArrayList<Student> arList) {
		this.arList = arList;
	}

	@Override
	public void addItem(Student item) {
		arList.add(item);
	}

	@Override
	public void delItem(Student item) {
		arList.remove(item);
	}

	@Override
	public void updateItem(Student item) {
		if(searchItem(item)){
			arList.set(arList.indexOf(item), item);
		}else{
			System.out.println("수정 실패");
		}
	}

	@Override
	public boolean searchItem(Student item) {
		return arList.contains(item);
	}

	@Override
	public void prnItems() {
		prnHeader();
		//Collections.sort(arList);
		for(Student s: arList){
			System.out.println(s);
		}
		System.out.println();
	}

	private void prnHeader() {
		String str = String
		.format("%3s\t%5s\t%3s\t%3s\t%3s\t%5s\t%5s",
				"학번", "성명", "국어", "영어", "수학", "총점", "평균");
		System.out.println(str);
		System.out.println("------------------------------------------------------");
	}

	@Override
	public void prnItems(Comparator<Student> comp) {
		Collections.sort(arList, comp);
		prnItems();
	}

}
